﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 书易.Model;
using 书易.DAL;

namespace 书易.WEB
{
    public partial class consignee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rep_GWC.DataSource = TradeDal.SelectYHSP(1);
                rep_GWC.DataBind();
            }
        }
        protected void LinkButton1_Click1(object sender, EventArgs e)
        {

        }


        protected void rep_GWC_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            Literal lit_price = e.Item.FindControl("lit_price") as Literal;
            Trade t = e.Item.DataItem as Trade;
            lit_price.Text = (t.BPrice * t.BCount).ToString() + "元";
            lit_sumP.Text += t.BPrice * t.BCount;


            List<OrderDetails> orderD = new List<OrderDetails>();
            OrderDetails od = new OrderDetails();
            od.BID = t.BID;
            od.BPrice = t.BPrice;
            od.BCount = t.BCount;
            od.Tid = t.TID;
            orderD.Add(od);
            Session["o"] = orderD;
        }

        protected void rep_GWC_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "sc")
            {
                Response.Redirect("ShanCSP.aspx?id=" + e.CommandArgument + "&URL=" + Server.UrlEncode(Request.Url.ToString()));
            }
        }

       
      
        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            

            List<OrderDetails> orderD = Session["o"] as List<OrderDetails>;

            if (orderD != null)
            {
                Orders os = new Orders();
                os.OID = OrdersDal.addDDH();
                os.MID = 1;
                os.ODate = DateTime.Now;
                os.OConsignee = txt_Name.Text;
                os.OAddress = txt_addr.Text;
                os.OTelephone = txt_tel.Text;
                os.OSumPrice = Convert.ToDouble(lit_sumP.Text);

                foreach (OrderDetails o in orderD)
                {
                    o.OID = os.OID;

                    int i = OrderDetailsDal.add(o);

                    if (i > 0)
                    {
                        TradeDal.del(Convert.ToInt32(o.Tid));
                    }
                }



                if (OrdersDal.add(os) > 0) Response.Redirect("done.aspx?dingDH=" + os.OID);
            }

        }
    }
}